<?php

namespace app\api\controller;

use app\api\model\group\GroupCategory;
use app\api\model\group\GroupMedia;
use app\api\model\group\GroupProjectApply;
use app\api\model\user\UserAuth;
use app\common\controller\Api;
use app\api\model\group\GroupOrder as GroupOrderModel;
use app\api\model\group\Group as GroupModel;

class Grouphead extends Api
{
    protected $noNeedRight = ['*'];

    //我的组团
    public function myGroup()
    {
        $list = GroupModel::where(['user_id'=>$this->auth->id])
            ->field('group_id,people_num,notice,name,status')
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $status_arr = [0=>'待审核',1=>'审核通过',2=>'审核拒绝',3=>'暂停'];
        foreach ($list as $k=>$v){
            $category_id =  GroupProjectApply::where(['group_id'=>$v['group_id']])->group('category_id')->column('category_id');
            $category_list = GroupCategory::where('id','in',$category_id)->column('name');
            $list[$k]['category_name'] = implode('/',$category_list);
            $list[$k]['status_text'] = $status_arr[$v['status']];
        }
        $this->success('',$list);
    }

    //删除
    public function del()
    {
        $group_id = input('group_id',0);
        $group_info = GroupModel::get(['group_id'=>$group_id,'user_id'=>$this->auth->id]);
        if (!$group_info)$this->error('抱团不存在');
        $group_info->delete();

        $this->success('删除成功');
    }

    //修改团长公告
    public function editnotice()
    {
        $notice = input('notice','');
        $group_id = input('group_id',0);
        $group_info = GroupModel::get(['group_id'=>$group_id,'user_id'=>$this->auth->id]);
        if (!$group_info)$this->error('抱团不存在');
        $group_info->notice = $notice;
        $group_info->save();
        $this->success('提交成功');
    }


    //成员列表
    public function groupOrder()
    {
        $group_id = input('group_id',0);
        $group_status = input('group_status','all');
        $pageSize = input('pageSize',10);
        $where = ['o.group_id'=>$group_id];
        if ($group_status!='all'){
            $where['o.group_status'] = $group_status;
        }
        $list = GroupOrderModel::alias('o')
            ->join('user u','o.user_id = u.id')
            ->where($where)
            ->field('o.order_id,o.user_id,o.group_id,o.realname,o.nickname,o.group_status,o.is_black,o.is_cancel,u.avatar as image')
            ->order('o.createtime desc')
            ->paginate($pageSize, false, ['query' => request()->get()]);

        $this->success('',$list);
    }

    //成员详情
    public function groupOrderInfo()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::where(['order_id'=>$order_id])
            ->field('order_id,user_id,group_status,refuse_reasons,createtime')
            ->find();

        $user_info = \app\api\model\User::where(['id'=>$info['user_id']])
            ->field('id,nickname,avatar,level_type,level,company_name,business_licence_number,company_found_date,
            company_address,company_attr,company_industry,company_business,company_people_num,company_mobile,company_intro,
            personal_intro,hobby,mobile,city,education,realname,gender,job,birthday,auth_status,person_auth,company_auth,
            company_auth_status')
            ->find();
        if (!$user_info)$this->error('成员不存在');
        if ($user_info['company_auth']==1){
            $user_info['is_company'] = 1;
        } else {
            //$user_info['age'] = $info['age'];
            $user_info['age'] = calculateAge($user_info['birthday']);
            $user_info['is_company'] = 0;
        }

        $user_model = new \app\api\model\User();
        $label = $user_model->getLevelLabelTextAttr($user_info);
        $user_info['level_label'] = $label;
        $level_list = $user_model->getLevelList();

        $user_info['level_text'] = $user_info['level']>0?$level_list[$user_info['level']]:'普通用户';
        $level_type_list = $user_model->getLeveTypelList();
        $user_info['level_type_text'] = $user_info['level_type']>0?$level_type_list[$user_info['level_type']]:'';
        $data = [
            'user_info'=>$user_info,
            'order_info'=>$info
        ];

        $this->success('',$data);
    }




    //审核
    public function audit()
    {
        $order_id = input('order_id',0);

        $status = input('status',1);

        $refuse_reasons = input('refuse_reasons','');

        $info = GroupOrderModel::get($order_id);
        //if ($info->group_status!=0)$this->error('error');
        $info->group_status = $status;
        $info->refuse_reasons = $refuse_reasons;
        $info->save();

        $this->success('审核成功');

        /*if ($info->save()){
            $this->success('审核成功');
        } else {
            $this->error('审核失败');
        }*/

    }

    //踢出
    public function kickOut()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->group_status!=1)$this->error('不是审核状态不能操作该事件');
        $info->group_status = 7;
        $info->save();
        $this->success('成功');
    }

    //暂停
    public function pause()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->group_status!=1)$this->error('不是审核状态不能操作该事件');
        $info->group_status = 3;
        $info->save();
        $this->success('成功');
        /*$group_id = input('group_id',0);
        $group_info = GroupModel::get(['group_id'=>$group_id,'user_id'=>$this->auth->id]);
        if (!$group_info)$this->error('抱团不存在');
        $group_info->status = 3;
        $group_info->save();

        $this->success('暂停成功');*/
    }

    //重启
    public function restart()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->group_status!=1)$this->error('不是暂停状态不能操作该事件');
        $info->group_status = 1;
        $info->save();
        $this->success('成功');
        /*$group_id = input('group_id',0);
        $group_info = GroupModel::get(['group_id'=>$group_id,'user_id'=>$this->auth->id]);
        if (!$group_info)$this->error('抱团不存在');
        $group_info->status = 1;
        $group_info->save();

        $this->success('重启成功');*/

    }

    //警告
    public function warning()
    {
        $order_id = input('order_id',0);

        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');
        if ($info->group_status!=1)$this->error('不是审核状态不能操作该事件');
        $info->group_status = 5;
        $info->save();
        $this->success('成功');

    }

    //拉黑/取消拉黑
    public function blacklist()
    {
        $order_id = input('order_id',0);
        $black = input('black',0);
        $info = GroupOrderModel::get($order_id);
        if (!$info)$this->error('报名记录不存在');

        if ($black){
            if ($info->group_status!=1)$this->error('不是审核状态不能操作该事件');
            $info->group_status = 6;
            $info->is_black = 1;
        } else {
            $info->group_status = 1;
            $info->is_black = 0;
        }

        $info->save();
        $this->success('成功');
    }

    public function info()
    {
        $group_id = input('group_id',0);

        $info = GroupModel::where(['group_id'=>$group_id])->find();

        $info['cert_img'] = GroupMedia::where(['group_id'=>$group_id,'type'=>0])
            ->field('media,title,describe')->select();

        $info['service_img'] = GroupMedia::where(['group_id'=>$group_id,'type'=>1])
            ->field('media,title,describe')->select();

        $info['service_video'] = GroupMedia::where(['group_id'=>$group_id,'type'=>2])
            ->field('media,title,describe')->select();

        $info['service_project'] = GroupProjectApply::where(['group_id'=>$group_id])
            ->field('id,project_id,category_id,name')->limit(1)->select();

        $this->success('',$info);
    }

}